package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Description
 * @Author bo.li
 * @Date 2023/3/16 10:51
 * @Version 1.0
 */
@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {
    @Autowired
    private WmChannelMapper channelMapper;
    @Autowired
    private WmNewsMapper wmNewsMapper;

    @Override
    public ResponseResult findAll() {
        return ResponseResult.okResult(list());
    }

    @Override
    public ResponseResult list(WmChannelDto dto) {
        //校验参数
        dto.checkParam();
        //分页查询
        Page<WmChannel> page = new Page<>(dto.getPage(), dto.getSize());
        //构造查询条件
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        //按照频道名称模糊查询
        wrapper.like(dto.getName() != null, WmChannel::getName, dto.getName());
        //按照创建时间倒序排序
        wrapper.orderByDesc(WmChannel::getCreatedTime);
        //启用禁用状态查询
        wrapper.eq(dto.getStatus() != null,WmChannel::getStatus, dto.getStatus());
        //执行查询
        page(page, wrapper);
        return ResponseResult.okResult(page.getRecords());
    }

    @Override
    public ResponseResult delete(Integer id) {
        //根据id查询频道信息
        WmChannel wmChannel = channelMapper.selectById(id);
        Boolean status = wmChannel.getStatus();
        //判断状态是否禁用状态
        if (status==false) {
            channelMapper.deleteById(id);
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_ACTIVE);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult add(WmChannel wmChannel) {
        //查询频道名称 是否重复
        WmChannel name = channelMapper.selectOne(new LambdaQueryWrapper<WmChannel>().eq(WmChannel::getName, wmChannel.getName()));
        if (name != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //频道名称
        wmChannel.setName(wmChannel.getName());
        //添加频道状态
        wmChannel.setStatus(wmChannel.getStatus());
        //频道描述
        wmChannel.setDescription(wmChannel.getDescription());
        //添加时间
        wmChannel.setCreatedTime(DateTime.now().toDate());
        //排序
        wmChannel.setOrd(wmChannel.getOrd());
        channelMapper.insert(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult updata(WmChannel wmChannel) {
        //查询自媒体图文内容信息表是否引用频道id
        LambdaQueryWrapper<WmNews> wrapper = new LambdaQueryWrapper();
        wrapper.eq(WmNews::getChannelId, wmChannel.getId());
        List<WmNews> wmNews = wmNewsMapper.selectList(wrapper);
        if (!CollectionUtils.isEmpty(wmNews)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_CITED);
        }
        channelMapper.updateById(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}